//
//  LoginVC.swift
//  Auscultation
//
//  Created by 来 on 2020/11/27.
//

import UIKit

class LoginVC: BaseVC {
    
    var view1: UIImageView!
    var view2: UIImageView!
    
    var dl_label: UILabel!
    var zc_label: UILabel!
    
    var yhm: LGFLoginImageTitleView!
    var mm: LGFLoginImageTitleView!
    var yzm: LGFLoginImageTitleView!
    
    var loginBtn: UIButton!
    var retPwdBtn: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        denglu()
    }
    
    @objc func login(_ sender: UIButton) {
        if yhm.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请输入用户名/手机号/账号")
            return
        }
        if mm.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请输入密码")
            return
        }
        
        if sender.lgf_ViewName == "登陆" {
            let paramet: [String: String] = [
                "loginName" : yhm.textField.text!,
                "loginType" : "2",
                "password" : mm.textField.text!,
                "scope" : "1"]
            EWERequest.lgf("login", paramet, .post, JSONEncoding.default) { (data) in
                EWEUser.lgf.wsxxuserId = data["userId"] as? String
                EWEUser.lgf.accessToken = data["accessToken"] as? String
                if EWEUser.lgf.accessToken.count == 0 {
                    let vc = XxwsVC()
                    vc.type = 1
                    self.navigationController?.show(vc, sender: self)
                    return
                }
                EWEUser.lgf.abbreviation = data["abbreviation"] as? String
                EWEUser.lgf.avatar = data["avatar"] as? String
                EWEUser.lgf.hospitalId = data["hospitalId"] as? String
                EWEUser.lgf.loginName = data["loginName"] as? String
                
                if let eduLevel = data["eduLevel"] as? Int{
                    
                    EWEUser.lgf.eduLevel = "\(eduLevel)"
                }
                EWEUser.lgf.exam = data["exam"] as? String

                
                let paramet: [String: String] = [
                    "encryptedData": "zFoamlSX95gBI7S05+e6HakuGltzhbckTNdkY0yZ2hU="
                ]
                EWERequest.lgf("login", paramet, .get) { (data) in
                    EWEUser.lgf.userId = data["userId"] as? String
                    EWEUser.lgf.realName = data["realName"] as? String
                    EWEUser.lgf.avatar = data["avatar"] as? String
                    EWEUser.lgf.menuList = String.lgf_JsonFrom((data["menuList"] as? [[String: Any]]) ?? [])
                    let appDelegate = UIApplication.shared.delegate as! AppDelegate
                    appDelegate.window?.rootViewController = MainNaviVC.init(rootViewController: MainVC())
                    let paramet2: [String: Any] = [
                        "limit": "100",
                    ]
                    EWERequest.lgf("hospital", paramet2, .get) { (data) in
                        if let hospitals = data["result"] as? [[String: Any]] {
                            hospitals.forEach { (hospital) in
                                let id = (hospital["id"] as? String ?? "")
                                let hospitalName = (hospital["hospitalName"] as? String ?? "")
                                if id == EWEUser.lgf.hospitalId {
                                    EWEUser.lgf.hospitalName = hospitalName
                                    return
                                }
                            }
                        }
                    } _: { (error) in
                        SVProgressHUD.showError(error)
                    }
                } _: { (error) in
                    SVProgressHUD.showError(error)
                }
            } _: { (error) in
                SVProgressHUD.showError(error)
            }
        } else if sender.lgf_ViewName == "注册" {
            if yzm.textField.text!.lgf_IsBlank() {
                SVProgressHUD.showError("请输入验证码")
            }
            let paramet: [String: String] = [
                "phone" : yhm.textField.text!,
                "password" : mm.textField.text!,
                "code" : yzm.textField.text!]
            EWERequest.lgf("signup", paramet, .post, JSONEncoding.default) { [weak self] (data) in
                self?.yzm.textField.text = ""
                self?.denglu()
            } _: { (error) in
                SVProgressHUD.showError(error)
            }
        }
    }
    
    @objc func yanzhengma(_ sender: UIButton) {
        var num = 60
        sender.isEnabled = false
        let timer = Timer.lgf_RunTimer(self, S: 1.0) { (timer) in
            if num == 1 {
                timer?.lgf_StopTimer()
                sender.isEnabled = true
                sender.setTitle("获取验证码", for: .normal)
            } else {
                num -= 1
                sender.setTitle(num.description + "s", for: .normal)
            }
        }
        
        let paramet: [String: String] = [
            "phone" : yhm.textField.text!,
            "type" : "0"]
        EWERequest.lgf("sms", paramet, .post, JSONEncoding.default) { (data) in
        } _: { (error) in
            timer.lgf_StopTimer()
            sender.isEnabled = true
            sender.setTitle("获取验证码", for: .normal)
            SVProgressHUD.showError(error)
        }
    }

    @objc func denglu() {
        view1.isHidden = false
        view2.isHidden = true
        yzm.isHidden = true
        yzm.snp.updateConstraints { (make) in
            make.height.equalTo(0)
        }
        loginBtn.setTitle("登陆", for: .normal)
        loginBtn.lgf_ViewName = "登陆"
        zc_label.textColor = UIColor.init(lgf_HexString: "CFD4D9")
        dl_label.textColor = UIColor.init(lgf_HexString: "222222")
    }
    
    @objc func zhuce() {
        view1.isHidden = true
        view2.isHidden = false
        yzm.isHidden = false
        yzm.snp.updateConstraints { (make) in
            make.height.equalTo(44.lgf_H())
        }
        loginBtn.setTitle("注册", for: .normal)
        loginBtn.lgf_ViewName = "注册"
        dl_label.textColor = UIColor.init(lgf_HexString: "CFD4D9")
        zc_label.textColor = UIColor.init(lgf_HexString: "222222")
    }
    
    @objc func retPwdBtn(_ sender: UIButton) {
        let vc = ResetpwdViewController()
        vc.resetSuccessClouse = {[weak self] phone,pwd in
            guard let self = self else { return }
            self.yhm.textField.text = phone
            self.mm.textField.text! = pwd

        }
        self.navigationController?.pushViewController(vc)
    }

}
